<template>
  <div ref="wrapRef" :style="{ height, width }"></div>
</template>
<script lang="ts">
  import { defineComponent, ref, nextTick, unref, onMounted } from 'vue';

  import { useScript } from '/@/hooks/web/useScript';
  import { useLoading } from '/@/components/Loading';


  const BAI_DU_MAP_URL =
    'https://api.map.baidu.com/getscript?v=3.0&ak=PIbaazbym2SUaxYW2RyOhtEAN0K3pmRI&services=&t=20210201100830&s=1';
  export default defineComponent({
    name: 'BaiduMap',
    props: {
      width: {
        type: String,
        default: '100%',
      },
      height: {
        type: String,
        default: 'calc(100vh - 78px)',
      },
    },
    setup() {
      const wrapRef = ref<HTMLDivElement | null>(null);
      const { toPromise } = useScript({ src: BAI_DU_MAP_URL });
      const [openFullLoading, closeFullLoading] = useLoading({
        tip: '地图加载中...',
      });
      /*async function initMap2() {
        await toPromise();
        await nextTick();
        const wrapEl = unref(wrapRef);
        if (!wrapEl) return;
        const BMap = (window as any).BMap;
        const map = new BMap.Map(wrapEl);
        const point = new BMap.Point(116.404, 39.915);
        map.centerAndZoom(point, 15);
        map.enableScrollWheelZoom(true);
      }*/

      function openFnFullLoading() {
        // alert("加载中");
        openFullLoading();

        setTimeout(() => {
          closeFullLoading();
        }, 2000);
      }

      async function initMap() {
        await toPromise();
        await nextTick();
        const wrapEl = unref(wrapRef);
        if (!wrapEl) return;
        const BMap = (window as any).BMap;
        const map = new BMap.Map(wrapEl);

        openFnFullLoading();
        // 百度地图API功能
        var geolocation = new BMap.Geolocation();
        geolocation.getCurrentPosition(function (r) {
          if (this.getStatus() == BMAP_STATUS_SUCCESS) {
            console.log("当前坐标:",r.point.lng + "__" + r.point.lat);
            var marker = new BMap.Marker(r.point);        // 创建标注
            map.addOverlay(marker);                     // 将标注添加到地图中
            getAddress(r.point.lng, r.point.lat);
          }
          else {
            console.log("获取当前坐标：超时")
            // alert('failed' + this.getStatus());
          }
        }, { enableHighAccuracy: true });
        //关于状态码
        //BMAP_STATUS_SUCCESS	检索成功。对应数值“0”。
        //BMAP_STATUS_CITY_LIST	城市列表。对应数值“1”。
        //BMAP_STATUS_UNKNOWN_LOCATION	位置结果未知。对应数值“2”。
        //BMAP_STATUS_UNKNOWN_ROUTE	导航结果未知。对应数值“3”。
        //BMAP_STATUS_INVALID_KEY	非法密钥。对应数值“4”。
        //BMAP_STATUS_INVALID_REQUEST	非法请求。对应数值“5”。
        //BMAP_STATUS_PERMISSION_DENIED	没有权限。对应数值“6”。(自 1.1 新增)
        //BMAP_STATUS_SERVICE_UNAVAILABLE	服务不可用。对应数值“7”。(自 1.1 新增)
        //BMAP_STATUS_TIMEOUT	超时。对应数值“8”。(自 1.1 新增)
        //通过经纬度获取地址信息
        function getAddress(lng, lat) {
          var myGeo = new BMap.Geocoder();
          var currentPoint = new BMap.Point(lng, lat);
          map.centerAndZoom(currentPoint,18);
          map.enableScrollWheelZoom(true);
          // 根据坐标得到地址描述
          myGeo.getLocation(currentPoint, function (result) {
            if (result) {
              var province = result.addressComponents.province;
              var city = result.addressComponents.city;
              var detail = result.address;
              console.log(province);
              console.log(city);
              console.log(detail);
            }
          });
        }

      }


      onMounted(() => {
        initMap();
      });

      return { wrapRef };
    },
  });
</script>
